package retrofitdemo.com.night.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;
import retrofitdemo.com.night.R;
import retrofitdemo.com.night.adapter.FloorAdapter;
import retrofitdemo.com.night.bean.FloorBean;
import retrofitdemo.com.night.databinding.ActivityFloorDescBinding;
import retrofitdemo.com.night.utils.GsonUtil;
import retrofitdemo.com.night.utils.HTTPAPI;
import retrofitdemo.com.night.utils.LogUtils;
import retrofitdemo.com.night.utils.OnItemClickListener;
import zxing.activity.CaptureActivity;

import static retrofitdemo.com.night.bean.ApiService.LOGINPRESSION;

public class FloorDescActivity extends BaseActivity {

    ActivityFloorDescBinding binding;
    List<FloorBean.DataBean> list = new ArrayList<>();
    List<FloorBean.DataBean> listSecond = new ArrayList<>();
    FloorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_floor_desc);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        setLeftVis();
        setTitleText("楼层");
        setRightText("扫码");
        binding.rlModule.setLayoutManager(new LinearLayoutManager(this));
        binding.rlModule.setItemAnimator(new DefaultItemAnimator());
        adapter = new FloorAdapter(this, listSecond);
        binding.rlModule.setAdapter(adapter);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(FloorDescActivity.this, FloorMoreImageActivity.class);
                intent.putExtra("module_floorId", listSecond.get(position).getId() + "");
                intent.putExtra("module_floor_name", listSecond.get(position).getTitle());
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        //获取楼层
        HTTPAPI.getInstance().getBWGFloors(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("获取楼层成功-->" + s);
                try {
                    FloorBean bean = GsonUtil.GsonToBean(s, FloorBean.class);
                    list.clear();
                    for (FloorBean.DataBean dataBean : bean.getData()) {
                        list.add(dataBean);
                    }
                    listSecond.addAll(list);
                    adapter.notifyDataSetChanged();
                } catch (Exception e) {

                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rightText:
                startActivityForResult(new Intent(FloorDescActivity.this, CaptureActivity.class), CaptureActivity.ACTION_QR);
                break;
            default:
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CaptureActivity.ACTION_QR && data != null) {
            String result = data.getStringExtra("result");
            LogUtils.e("扫码-->" + result);
            //http://www.fuqixuan.top/bowuguan/api/finddpapibyid?id=3
            Intent intent = new Intent( FloorDescActivity.this, FloorMoreImageDescActivity.class );
            intent.putExtra( "module_floorId", result.split("id=")[1] );
            startActivity( intent );
        }else if(requestCode == LOGINPRESSION ){
            Toast.makeText(FloorDescActivity.this, "有结果", Toast.LENGTH_SHORT).show();
            startActivityForResult(new Intent(FloorDescActivity.this, CaptureActivity.class), CaptureActivity.ACTION_QR);
        }
    }
}
